﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Npgsql;

namespace 第十一次作业
{
    public partial class 单词斩 : Form
    {
        private readonly string connectionString = "Host=localhost;Username=postgres;Password=chester7.20;Database=单次";
        private string currentEnglishWord;

        public 单词斩()
        {
            InitializeComponent();
            LoadRandomWord();
        }

        private void submit_Click(object sender, EventArgs e)
        {

                if (textBox3.Text.Trim().Equals(currentEnglishWord, StringComparison.OrdinalIgnoreCase))
                {
                   textBox4.Text = "√";
                }
                else
                {
                   textBox4.Text = "X";
                }
    
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LoadRandomWord();
        }
        private void LoadRandomWord()
        {
            using (var conn = new NpgsqlConnection(connectionString))
            {
                conn.Open();
                var cmd = new NpgsqlCommand("SELECT english_word, chinese_meaning FROM words ORDER BY RANDOM() LIMIT 1", conn);
                var reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    currentEnglishWord = reader.GetString(0);
                    textBox1.Text = reader.GetString(1);
                    textBox3.Clear();
                    textBox4.Text = "";
                }
                reader.Close();
            }
        }

    }
}
